package com.xjrsoft.module.fabricapply.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.bi.common.tool.DateUtil;
import com.xjrsoft.module.fabric.FabricConfig;
import com.xjrsoft.module.fabricapply.entity.FabricApply;
import com.xjrsoft.module.fabricapply.mapper.FabricApplyMapper;
import com.xjrsoft.module.fabricapply.service.IFabricApplyService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @title: service
 * @Author 管理员
 * @Date: 2024-12-17
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class FabricApplyServiceImpl extends ServiceImpl<FabricApplyMapper, FabricApply> implements IFabricApplyService {

    @Resource
    private FabricConfig fabricConfig;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(FabricApply fabricApply) {
        fabricApply.setCreateDate(new Date());
        boolean flag = this.save(fabricApply);
        if (flag) {
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org1", "add", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
                fabricApply.setApplyTransactionHash(txId);
                fabricApply.setApplyTime(new Date());
                updateApplyHash(fabricApply);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(FabricApply fabricApply) {
        boolean flag = this.updateById(fabricApply);
        if (flag) {
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org1", "edit", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(List<Long> ids) {
        boolean flag = this.removeBatchByIds(ids);
        if (flag) {
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org1", "delete", String.valueOf(ids.get(0)), String.valueOf(ids.get(0)));
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String firstApproval(FabricApply fabricApply) {
        fabricApply.setFirstApprovalTime(new Date());
        boolean flag = this.updateById(fabricApply);
        if (flag) {
            fabricApply.setFirstTime(new Date());
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org1", "edit", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
                fabricApply.setFirstTransactionHash(txId);
                fabricApply.setFirstTime(new Date());
                updateApplyHash(fabricApply);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String finalApproval(FabricApply fabricApply) {
        fabricApply.setFinalApprovalTime(new Date());
        boolean flag = this.updateById(fabricApply);
        if (flag) {
            fabricApply.setFinalTime(new Date());
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org2", "edit", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
                fabricApply.setFinalTransactionHash(txId);
                fabricApply.setFinalTime(new Date());
                updateApplyHash(fabricApply);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String transactCertificate(FabricApply fabricApply) {
        fabricApply.setCertificateStatus("YZZ");
        fabricApply.setLicNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtils.nextInt(10, 100000));
        boolean flag = this.updateById(fabricApply);
        if (flag) {
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org2", "edit", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String sendCertificate(FabricApply fabricApply) {
        fabricApply.setCertificateStatus("YFZ");
        fabricApply.setSendDate(new Date());
        boolean flag = this.updateById(fabricApply);
        if (flag) {
            String txId = "";
            try {
                // 生成区块链交易
                txId = fabricConfig.cohain("org2", "edit", fabricApply.getId().toString(), JSONObject.toJSONString(fabricApply));
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("区块链上链失败，原因:" + e.getMessage());
            }
            return "上链成功，交易ID:" + txId;
        }
        return null;
    }

    public void updateApplyHash(FabricApply fabricApply) {
        this.updateById(fabricApply);
    }
}
